using System.Collections.Generic;
using Nekonya.Dicts.Model;
using Nekonya.Dicts.Tpl;
using TinaX;
using TinaX.I18N;

namespace Nekonya.Dicts
{
    [DictInfo("dict_item")]
    public class ItemDict : DictBase<ItemTpl>
    {
        private const string c_I18NGroupName = "item";
        private readonly List<ItemModel> m_Items = new List<ItemModel>();
        private readonly Dictionary<int, ItemModel> m_DictItems = new Dictionary<int, ItemModel>();

        [Inject]
        public II18N I18N { get; set; }

        public override void DataCtor(IEnumerable<ItemTpl> data)
        {
            foreach(var tpl in data)
            {
                var item = new ItemModel
                {
                    ID = tpl.ID,
                    Type = tpl.Type,
                    Name = tpl.Name,
                    Description = tpl.Description,
                    Icon = tpl.Icon,
                    Quality = tpl.Quality,
                };

                m_Items.Add(item);
                m_DictItems.Add(item.ID, item);
            }
        }

        public ItemModel Get(int Id)
            => m_DictItems[Id];

        /// <summary>
        /// 获取全部，
        /// 如果数据量特别大的话，慎用这个功能
        /// </summary>
        /// <returns></returns>
        public ItemModel[] GetAll()
            => m_Items.ToArray();

        /// <summary>
        /// 获取显示名
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string GetName(ItemModel model)
        {
            return I18N.GetText(model.Name, c_I18NGroupName, model.Name);
        }

        public string GetDescription(ItemModel model)
        {
            return I18N.GetText(model.Description, c_I18NGroupName, model.Description);
        }
    }
}
